from django.shortcuts import render

# Create your views here.

from django.contrib.auth.hashers import make_password, check_password
from rest_framework.views import APIView
from rest_framework.response import Response

from .serializer import *

import re

from django.conf import settings  # 要用settings
from itsdangerous import TimedJSONWebSignatureSerializer  # token要用

serializers_user = TimedJSONWebSignatureSerializer(settings.SECRET_KEY, 86400)  # 加密  保存时间秒


# Create your views here.


class RegisterView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password1 = request.data.get('password1')
        password2 = request.data.get('password2')
        phone = request.data.get('phone')
        img = request.data.get('img')
        if not re.match(r'1[35678]\d{9}$', phone):
            return Response({'msg': '电话号码不规范', 'code': 400})
        user_obj = UserModel.objects.filter(username=username).first()
        if user_obj:
            return Response({'msg': '该用户已存在', 'code': 400})
        if password2 == password1:
            data = {
                'username': username,
                'password': make_password(password1),
                'phone': phone,
                'img': img
            }
            ser = UserSer(data=data)
            if ser.is_valid():
                ser.save()
                return Response({'msg': '注册成功', 'code': 200})
            else:
                print(ser.errors)
                return Response({'msg': '注册失败', 'code': 400})
        else:
            return Response({'msg': '两次密码不一致', 'code': 400})


class LoginView(APIView):
    def post(self, request):
        username = request.data.get('username')
        password = request.data.get('password')
        user_obj = UserModel.objects.filter(username=username).first()
        if user_obj:
            if check_password(password, user_obj.password):
                user_info = {'user_id': user_obj.pk}  # user的id
                token = serializers_user.dumps(user_info).decode()  # dumps以后是bytes类型，需要用decode转换一下
                return Response({'msg': '登录成功', 'code': 200, 'token': token})  # 登录成功后吧token传到前端
            else:
                return Response({'msg': '用户名或密码错误', 'code': 400})
        else:
            return Response({'msg': '用户名或密码错误', 'code': 400})


class UpdateInfoView(APIView):
    def post(self, request):
        token = request.data.get('token')
        user_info = serializers_user.loads(token)
        user_id = user_info.get('user_id')
        user_obj = UserInfoModel.objects.filter(user=user_id).first()
        data = {
            'email': request.data.get('email'),
            'sex': request.data.get('sex'),
            'age': request.data.get('age'),
            'site': request.data.get('site'),
            'x_site': request.data.get('x_site'),
            'user': user_id,
        }
        ser = UserInfoSer(user_obj, data=data)
        if ser.is_valid():
            ser.save()
            return Response({'msg': '修改成功', 'code': 200})
        else:
            return Response({'msg': '修改失败', 'code': 400, 'errors': ser.errors})
